<?php

/**
 * This is the model class for table "{{establecimiento}}".
 *
 * The followings are the available columns in table '{{establecimiento}}':
 * @property integer $ID_ESTABLECIMIENTO
 * @property string $NOMBRE
 * @property string $DOMICILIO
 * @property string $RFC
 * @property string $IMSS
 * @property integer $ID_EMPRESA
 * @property integer $ACTIVO
 *
 * The followings are the available model relations:
 * @property ComisionMixtaCap[] $tblComisionMixtaCaps
 * @property Plan[] $tblPlans
 */
class Establecimiento extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Establecimiento the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{establecimiento}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('ID_EMPRESA', 'required'),
			array('ID_EMPRESA, ACTIVO', 'numerical', 'integerOnly'=>true),
			array('NOMBRE', 'length', 'max'=>100),
			array('DOMICILIO', 'length', 'max'=>300),
			array('RFC', 'length', 'max'=>13),
			array('IMSS', 'length', 'max'=>20),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('ID_ESTABLECIMIENTO, NOMBRE, DOMICILIO, RFC, IMSS, ID_EMPRESA, ACTIVO', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'tblComisionMixtaCaps' => array(self::MANY_MANY, 'ComisionMixtaCap', '{{comision_establecimiento}}(ID_ESTABLECIMIENTO, ID_COMISION_MIXTA)'),
			'tblPlans' => array(self::MANY_MANY, 'Plan', '{{plan_establecimiento}}(ID_ESTABLECIMIENTO, ID_PLAN)'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'ID_ESTABLECIMIENTO' => 'Id Establecimiento',
			'NOMBRE' => 'Nombre',
			'DOMICILIO' => 'Domicilio',
			'RFC' => 'Rfc',
			'IMSS' => 'Imss',
			'ID_EMPRESA' => 'Id Empresa',
			'ACTIVO' => 'Activo',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		
			
		$criteria=new CDbCriteria;

		$criteria->compare('ID_ESTABLECIMIENTO',$this->ID_ESTABLECIMIENTO);
		$criteria->compare('NOMBRE',$this->NOMBRE,true);
		$criteria->compare('DOMICILIO',$this->DOMICILIO,true);
		$criteria->compare('RFC',$this->RFC,true);
		$criteria->compare('IMSS',$this->IMSS,true);		
		$criteria->compare('ACTIVO',$this->ACTIVO);

		$empresas = EmpresaUsuario::model()->findAllByAttributes(array('ID_USUARIO'=>Yii::app()->user->id, 'ACTIVO'=>1));
		
		$criteria=new CDbCriteria;
		
		foreach ($empresas as $empresa)
			//$criteria->compare('id_empresa',$empresa->ID_EMPRESA , true);
		
		
		$criteria->compare('ID_EMPRESA',$empresa->ID_EMPRESA, true);
		
		
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}